package top.hcode.hoj.service.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nutz.lang.Strings;
import org.springframework.stereotype.Component;
import top.hcode.hoj.dao.course.SampleDao;
import top.hcode.hoj.pojo.dto.QuerySampleDTO;
import top.hcode.hoj.pojo.entity.course.Sample;

/**
 * @author orangej
 * @since 2025/1/4
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class AdminSampleService {
    private final SampleDao sampleDao;

    public IPage<Sample> getSampleList(QuerySampleDTO querySampleDTO) {
        IPage<Sample> pager = new Page<>(querySampleDTO.getCurrentPage(), querySampleDTO.getLimit());

        QueryWrapper<Sample> query = new QueryWrapper<>();
        if (Strings.isNotBlank(querySampleDTO.getKeyword())) {
            query.like("title", querySampleDTO.getKeyword());
        }
        sampleDao.page(pager, query);
        return pager;
    }

    public Sample getSample(Long id) {
        return sampleDao.getById(id);
    }

    public Sample saveSample(Sample sample) {
        sampleDao.saveOrUpdate(sample);
        return sample;
    }

    public void deleteSample(Long id) {
        sampleDao.removeById(id);
    }
}
